export function filterEmptyKey (obj = {}) {
  for (const key in obj) {
    // 去除空格
    if (typeof obj[key] === 'string') {
      obj[key] = String(obj[key]).trim()
    }
    if (Object.prototype.hasOwnProperty.call(obj, key) && (obj[key] !== 0) && (obj[key] === null || obj[key] === '')) {
      delete obj[key]
    }
  }
  return obj
}

export function getMenu (menus = [], url) {
  for (let i = 0; i < menus.length; i++) {
    if (url.includes(menus[i].url) && menus[i].url) {
      return menus[i]
    } else if (menus[i].childNode && menus[i].childNode.length) {
      const menu = getMenu(menus[i].childNode, url)
      if (menu) return menu
    }
  }
}

export function handleFenceGroups (points = []) {
  const fenceMap = new Map()
  points.forEach(item => {
    const { fenceCode, longitude, latitude } = item
    const loaction = fenceMap.get(fenceCode)
    if (loaction) {
      loaction.push({ longitude, latitude })
    } else {
      fenceMap.set(fenceCode, [{ longitude, latitude }])
    }
  })
  return Array.from(fenceMap)
}

// 处理经纬度数据
export function handleTLnglatList (point) {
  const fenceCode = []
  const drawPolyonList = []
  const setPolyonList = []
  point.forEach(item => {
    if (!fenceCode.includes(item.fenceCode)) {
      fenceCode.push(item.fenceCode)
    }
  })
  fenceCode.forEach(item => {
    drawPolyonList.push([])
    setPolyonList.push([])
    const drawPolyonPoints = []
    const setPolyonPoints = []
    point.forEach(item1 => {
      if (item === item1.fenceCode) {
        drawPolyonPoints.push(new T.LngLat(item1.longitude, item1.latitude))
        setPolyonPoints.push({
          longitude: item1.longitude,
          latitude: item1.latitude
        })
      }
    })
    drawPolyonList[drawPolyonList.length - 1] = drawPolyonPoints
    setPolyonList[setPolyonList.length - 1] = setPolyonPoints
  })
  return {
    drawPolyonList: drawPolyonList, // 天地图绘制格式
    setPolyonList: setPolyonList, // 设置的经纬度传参格式
    fenceCode: fenceCode // 对应电子围栏的code
  }
}
// 加
export function addNum (num1, num2) {
  let sq1, sq2
  try {
    sq1 = num1.toString().split('.')[1].length
  } catch (e) {
    sq1 = 0
  }
  try {
    sq2 = num2.toString().split('.')[1].length
  } catch (e) {
    sq2 = 0
  }
  const m = Math.pow(10, Math.max(sq1, sq2))
  return (num1 * m + num2 * m) / m
}
// 减
export function reduceNum (arg1, arg2, num) { // num为保留位数,不传时不四舍五入
  arg1 = Number(arg1)
  arg2 = Number(arg2)
  let r1 = 0; let r2 = 0
  try { r1 = arg1.toString().split('.')[1].length } catch (e) {}
  try { r2 = arg2.toString().split('.')[1].length } catch (e) {}
  const m = Math.pow(10, Math.max(r1, r2))
  if (num || num === 0) {
    return ((arg1 * m - arg2 * m) / m).toFixed(num) // 注意：fixed四舍五入大于5会进1 5并不会进1
  }
  return (arg1 * m - arg2 * m) / m
}

/**
 *var latlngs = [
 *    [40.41646957397461, 115.95511436462402],
 *    [40.411319732666016, 115.95399856567383],
 *    [40.40994644165039, 115.9647274017334],
 *    [40.416555404663086, 115.96558570861816]
 *  ]
 * 输入一组坐标计算中心点
 */
export function mapGetCenter (pointArray) {
  const sortedLongitudeArray = pointArray.map(item => item[1]).sort()
  const sortedLatitudeArray = pointArray.map(item => item[0]).sort()
  const centerLongitude = ((parseFloat(sortedLongitudeArray[0]) + parseFloat(sortedLongitudeArray[sortedLongitudeArray.length - 1])) / 2).toFixed(4)
  const centerLatitude = ((parseFloat(sortedLatitudeArray[0]) + parseFloat(sortedLatitudeArray[sortedLatitudeArray.length - 1])) / 2).toFixed(4)
  return [centerLongitude, centerLatitude]
}

/**
 * 将数字转成中文大写
 * @params num
 */
export function toChinesNum (num) {
  const changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  const unit = ['', '十', '百', '千', '万']
  num = parseInt(num)
  const getWan = (temp) => {
    const strArr = temp.toString().split('').reverse()
    let newNum = ''
    for (let i = 0; i < strArr.length; i++) {
      newNum = (i === 0 && strArr[i] === 0 ? '' : (i > 0 && strArr[i] === 0 && strArr[i - 1] === 0 ? '' : changeNum[strArr[i]] + (strArr[i] === 0 ? unit[0] : unit[i]))) + newNum
    }
    return newNum
  }
  const overWan = Math.floor(num / 10000)
  let noWan = num % 10000
  if (noWan.toString().length < 4) noWan = '0' + noWan
  return overWan ? getWan(overWan) + '万' + getWan(noWan) : getWan(num)
}

/**
 * px转vw，页面为vw布局，
 * @return px单位数值
*/
export function px2vw (px) {
  return Math.ceil(px * (document.documentElement.clientWidth / 1920))
}
